package com.xzq.bugstack.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/20 11:10
 * @Version 1.0.0
 **/
public class NettyServer {
    public static void main(String[] args) {
        new NettyServer().bind(9999);
    }

    private void bind(int i) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true) //广播
                    .option(ChannelOption.SO_RCVBUF, 2048 * 1048) //设置UDP读缓冲区位2M
                    .option(ChannelOption.SO_SNDBUF, 1024 * 1024) //设置UDP写缓冲区为1M
                    .handler(new MyChannelInitializer());

            ChannelFuture f = bootstrap.bind(i).sync();
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}
